import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Res,
  HttpCode,
  HttpStatus,
  Req,
  Logger,
} from '@nestjs/common'
import { Response, Request } from 'express'
import { OrderService } from './order.service'
import { CreateOrderDto } from './dto/create-order.dto'
import { UpdateOrderDto } from './dto/update-order.dto'
import { ConfigService } from '@nestjs/config'

@Controller('order')
export class OrderController {
  constructor(
    private readonly orderService: OrderService,
    private readonly configService: ConfigService,
    private readonly logger: Logger,
  ) {}

  @Post('login')
  @HttpCode(HttpStatus.OK)
  login(@Body() user, @Res({ passthrough: true }) res: Response) {
    console.log(this.configService.get('DATABASE_URL'))
    this.logger.log('请求成功', OrderController.name)
    this.logger.error('请求失败', { stack: [null] })
    this.logger.warn('请求警告', OrderController.name)

    res.cookie('token', 'token123123', {
      httpOnly: true,
      path: '/',
      maxAge: 24 * 60 * 60 * 1000,
      sameSite: 'none',
      secure: false,
    })
    return {
      message: '登录成功',
      user,
      token: 'token123123',
    }
  }

  // 添加检查cookie的端点
  @Get('check-cookie')
  checkCookie(@Req() req: Request) {
    console.log('服务器收到的cookies:', req.cookies)
    return {
      cookies: req.cookies,
      message: '这是服务器收到的所有cookie',
    }
  }

  @Post()
  create(@Body() createOrderDto: CreateOrderDto) {
    return this.orderService.create(createOrderDto)
  }

  @Get()
  findAll() {
    return this.orderService.findAll()
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.orderService.findOne(+id)
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateOrderDto: UpdateOrderDto) {
    return this.orderService.update(+id, updateOrderDto)
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.orderService.remove(+id)
  }
}
